package com.nest.WorkNest.act;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lidroid.xutils.exception.HttpException;
import com.nest.WorkNest.R;
import com.nest.WorkNest.adapter.HolderAdapter;
import com.nest.WorkNest.adapter.HolderAdapter.ViewCreator;
import com.nest.WorkNest.api.XutilsPost;
import com.nest.WorkNest.tools.Tool;
import com.orhanobut.logger.Logger;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class CategoryAct extends BaseActivity implements OnItemClickListener {
    private Context context = CategoryAct.this;
    private ListView lv_category;
    private GridView gv_category;
    private HolderAdapter<HashMap<String, String>> lAdapter, gAdapter;
    private List<HashMap<String, String>> data1 = new ArrayList<>();
    private List<HashMap<String, String>> data2 = new ArrayList<>();
    private int selectIndex;
    private String fatherType;
    private TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_category);
        loadProductType("");
        initView();
    }

    private void initView() {
        initBar();
        actionbar_side_name.setVisibility(View.VISIBLE);
        actionbar_side_name.setText("品类");
        ll_iv_actionbar_side_left.setVisibility(View.VISIBLE);
        back();
        textView = (TextView) findViewById(R.id.category_none);
        lv_category = (ListView) findViewById(R.id.lv_category);
        gv_category = (GridView) findViewById(R.id.gv_category);
        lv_category.setSelector(R.color.bg_transparent);
        lv_category.setOnItemClickListener(this);
        gv_category.setOnItemClickListener(this);
        createAdapter();
        lAdapter.update(data1);
        gAdapter.update(data2);
    }

    public void loadProductType(final String fatherType) {
        XutilsPost xutilsPost = new XutilsPost();
        XutilsPost.IOAuthCallBack ioAuthCallBack = new XutilsPost.IOAuthCallBack() {
            @Override
            public void getIOAuthCallBack(String result) {
                JSONObject jsonObject = Tool.getJsonObject(result);
                JSONArray jsonArray;
                Logger.json(result);
                if (Tool.getString(jsonObject, "resultCode").equals("0")) {
                    if (TextUtils.isEmpty(fatherType)) {
                        jsonArray = Tool.getJsonArray(result, "dataList");
                        data1 = Tool.getListMapByJsonArray(jsonArray);
                        lAdapter.update(data1);
                        loadProductType(data1.get(0).get("typeId"));
                    } else {
                        if (Tool.getString(jsonObject, "total").equals("0")) {
                            textView.setVisibility(View.VISIBLE);
                            gv_category.setVisibility(View.GONE);
                        } else {
                            jsonArray = Tool.getJsonArray(result, "dataList");
                            data2 = Tool.getListMapByJsonArray(jsonArray);
                            gAdapter.update(data2);
                        }

                    }

                } else if (Tool.getString(result, "resultCode").equals("9001")) {
                    showToast("token已过期，请重新登录");
                    Intent intent = new Intent(context, LoginAct.class);
                    startActivity(intent);
                    finish();
                }
            }

            @Override
            public void getDealException(HttpException httpException, String result) {

            }

            @Override
            public void getLoadingCallBack(long total, long current, boolean isUploading) {

            }
        };
        xutilsPost.doPostProductType(context, fatherType, ioAuthCallBack);
    }

    public void createAdapter() {
        ViewCreator<HashMap<String, String>> creatorl = new ViewCreator<HashMap<String, String>>() {

            @Override
            public View createView(LayoutInflater inflater, int position,
                                   HashMap<String, String> map) {

                return LayoutInflater.from(context).inflate(R.layout.items_listview_category, null);
            }

            @Override
            public void updateView(View view, int position, HashMap<String, String> map) {
                RelativeLayout rl_category_items = (RelativeLayout) view
                        .findViewById(R.id.rl_category_items);
                ImageView iv_category_bar = (ImageView) view.findViewById(R.id.iv_category_bar);
                TextView tv_category_cname = (TextView) view.findViewById(R.id.tv_category_cname);
                tv_category_cname.setText(map.get("typeName"));
                if (getSelectIndex() == position) {
                    iv_category_bar.setVisibility(View.VISIBLE);
                    tv_category_cname.setTextColor(
                            context.getResources().getColor(R.color.mainfontcolor_orange));
                    rl_category_items.setBackgroundColor(Color.WHITE);
                } else {
                    iv_category_bar.setVisibility(View.GONE);
                    tv_category_cname.setTextColor(
                            context.getResources().getColor(R.color.mainfontColor_gray));
                    rl_category_items.setBackgroundColor(0xf3f3f3);
                }
            }
        };
        lAdapter = new HolderAdapter<>(this.getLayoutInflater(), creatorl);
        lv_category.setAdapter(lAdapter);
        lAdapter.update(data1);
        ViewCreator<HashMap<String, String>> creatorg = new ViewCreator<HashMap<String, String>>() {

            @Override
            public View createView(LayoutInflater inflater, int position,
                                   HashMap<String, String> map) {

                return LayoutInflater.from(context).inflate(R.layout.items_gridview_category, null);
            }

            @Override
            public void updateView(View view, int position, HashMap<String, String> map) {
                TextView tv_category_pname = (TextView) view.findViewById(R.id.tv_category_pname);
                tv_category_pname.setText(map.get("typeName"));
            }
        };
        gAdapter = new HolderAdapter<>(this.getLayoutInflater(), creatorg);
        gv_category.setAdapter(gAdapter);
        gAdapter.update(data2);
    }

    public int getSelectIndex() {
        return selectIndex;
    }

    public void setSelectIndex(int position) {
        this.selectIndex = position;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        switch (parent.getId()) {
            case R.id.lv_category:
                setSelectIndex(position);
                fatherType = data1.get(position).get("typeId");
                loadProductType(fatherType);
                lAdapter.update(data1);
                break;
            case R.id.gv_category:
                fatherType = data2.get(position).get("typeId");
                loadingHomeGridView(fatherType);
                break;
        }
    }

    private void loadingHomeGridView(String productType) {
        XutilsPost xutilsPost = new XutilsPost();
        XutilsPost.IOAuthCallBack ioAuthCallBack = new XutilsPost.IOAuthCallBack() {
            @Override
            public void getIOAuthCallBack(String result) {
                JSONObject jsonObject = Tool.getJsonObject(result);
                Logger.json(result);
                if (Tool.getString(jsonObject, "resultCode").equals("0")) {
                    Intent intent = new Intent(context, PlatformAct.class);
                    intent.putExtra("result", result);
                    startActivity(intent);
                } else if (Tool.getString(result, "resultCode").equals("9001")) {
                    showToast("token已过期，请重新登录");
                    Intent intent = new Intent(context, LoginAct.class);
                    startActivity(intent);
                    finish();
                } else {
                    showToast(Tool.getString(jsonObject, "MSG"));
                }
            }

            @Override
            public void getDealException(HttpException httpException, String result) {

            }

            @Override
            public void getLoadingCallBack(long total, long current, boolean isUploading) {

            }
        };
        xutilsPost.doPostProductSelectRecords(context, "1", productType, ioAuthCallBack);
    }
//    protected void onResume() {
//        loadProductStyle();
//        loadProductType();
//        super.onResume();
//    }
}
